app.controller('roleEditAuthController', ['$scope', 'httpWrapService', 'dialogs', '$state', '$stateParams',
                        '$location', '$anchorScroll', 'AlertFactory','validationService',
                function ($scope, httpWrapService, dialogs, $state, $stateParams, $location, $anchorScroll, AlertFactory,validationService) {
	$scope.entity = {};
	$scope.saveBtnDisable = false;
	$scope.selectAuths = {			
	};

    $scope.id = $stateParams.id;

	
	httpWrapService.post('system/role/editAuth/'+ $scope.id +'.do',{}).then(function(res2){
		$scope.originalAuths = res2.data.map.list;
		var authMaps = {};
		for(var index in $scope.originalAuths) {
			var permissionId = $scope.originalAuths[index].permissionId;
			authMaps[permissionId] = permissionId;
		}
		var datas = res2.data.data;
		for(var index in datas) {
			var data = datas[index];
			if(authMaps[data.id]) {
				data.checked = true;
				$scope.selectAuths[data.id] = data.id;
			}
		}
		$scope.auths = datas;
		
    }); 
	
	
	$scope.save = function(){
		httpWrapService.post('system/role/saveAuth/'+ $scope.id +'.do',{
			selectAuths : $scope.selectAuths
		}).then(function(){
			dialogs.notify('提示', '已经修改成功！', {size: 'sm'});
		});
	}
	
	$scope.setting = {
		check : {
			autoCheckTrigger : true,
			enable: true,
	        chkStyle: "checkbox",
	        chkboxType: {"Y": "p", "N": "s"}
		},
		callback : {
			onClick: function (event, treeId, treeNode, clickFlag) {
            },
            onCheck : function (event, treeId, treeNode){
            	if(treeNode.checked === true) {
            		$scope.selectAuths[treeNode.id] = treeNode.id;
            	} else if(treeNode.checked === false) {
            		delete $scope.selectAuths[treeNode.id];
            	}
            }
		}
	}

}]);
